package com.hulizuofang.wol.chat.server.handler;

import com.google.inject.Inject;
import com.gwtplatform.dispatch.server.ExecutionContext;
import com.gwtplatform.dispatch.server.actionhandler.ActionHandler;
import com.gwtplatform.dispatch.shared.ActionException;
import com.hulizuofang.wol.chat.client.action.SetLoginResult;
import com.hulizuofang.wol.chat.client.action.SetLoginAction;
import com.hulizuofang.wol.chat.client.action.SetLoginAction.LoginWorker;
import com.hulizuofang.wol.chat.client.ui.user.ChatUser;
import com.hulizuofang.wol.chat.server.ChatUserRepository;

public class CheckLoginActionHandler implements ActionHandler<SetLoginAction, SetLoginResult> {
	
	private final ChatUserRepository chatUserProvider;

	@Inject
	public CheckLoginActionHandler(ChatUserRepository chatUserProvider) {
		this.chatUserProvider = chatUserProvider;
	}
	
	@Override
	public SetLoginResult execute(SetLoginAction action,
			ExecutionContext context) throws ActionException {
		return new SetLoginResult(action.workLogin(new LoginWorker() {
			@Override
			public ChatUser work(String login) {
				return chatUserProvider.login(login);
			}
		}), chatUserProvider.getOnlineUsers());
	}

	@Override
	public Class<SetLoginAction> getActionType() {
		return SetLoginAction.class;
	}

	@Override
	public void undo(SetLoginAction action, SetLoginResult result,
			ExecutionContext context) throws ActionException {
	}

}
